package com.example.tests;

import static com.example.tests.TestBase.generateRandomString;
import static com.example.tests.TestBase.generateRandomDateYear;
import static com.example.tests.TestBase.generateRandomMonth;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		if (args.length < 3){
			System.out.println("Please specifi parametrs: <amount of fest data>"
                    + " <file> <format>");
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()){
			System.out.println("The file [ "+file+" ] is alredy exists, please remove if manually");
			return;
		}
		
		List<ContactData> contacts = generateRandomContact(amount);
		if ("csv".equals(format)){
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		}
		else {
			System.out.println("Unknow format: "+format);
		}
	}
	/*******************************************************************/
	
	private static List<ContactData> generateRandomContact(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();

		for (int i = 0; i < amount; i++){
			ContactData contact = new ContactData()
			 .withFirstname(generateRandomString())
			 .withLastname(generateRandomString())
			 .withFirstemail(generateRandomString())
			 .withHomephone(generateRandomString())
			 .withAddress(generateRandomString())
			 .withBirthday(generateRandomDateYear(30))
			 .withBirthmonth(generateRandomMonth())
			 .withBirthyear(generateRandomDateYear(2012));

			list.add(contact);
		}
		return list;
	}

	/**
	 * ******************************************************************
	 * XML
	 * */
	private static void saveContactsToXmlFile(List<ContactData> groups, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("group", ContactData.class);
		String xml = xstream.toXML(groups);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	public static List<ContactData> loadGroupsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("group", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	/**
	 * ******************************************************************
	 * CSV
	 * @throws IOException 
	 * */

	private static void saveContactsToCsvFile(List<ContactData> contacts,	File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for(ContactData contact : contacts){
			writer.write(contact.getFirstname()+","+
						 contact.getLastname()+","+
						 contact.getFirstemail()+","+
						 contact.getHomephone()+","+
						 contact.getBirthday()+","+
						 contact.getBirthmonth()+","+
						 contact.getBirthyear()+","+
						 contact.getAddress()+";\n");
		}
		writer.close();
	}

	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null){
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFirstname(part[0])
				.withLastname(part[1])
				.withFirstemail(part[2])
				.withHomephone(part[3])
				.withBirthday(part[4])
				.withBirthmonth(part[5])
				.withBirthyear(part[6])
				.withAddress(part[7]);
				
			list.add(contact);
			line = bufferedReader.readLine();
		}
		reader.close();
		return list;
	}

}
